<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 角色模型
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class RoleModel extends CommonModel{

	/**
     * 返回对象的定义
     */
    static public function init__(){

        return array(

            'table_name' => 'role',

            'props' => array(

                // 主键应该是只读，确保领域对象的“不变量”
               'role_id' => array( 'readonly' => true ),
            ),

            'attr_protected' => 'role_id',

            'check' => array(

               'role_name'=>array(

                     array( 'require',G::L( '应用名不能为空' ) ),
                     array( 'uniqueRoleName',G::L( '组名已经存在' ),'condition'=>'must','extend'=>'callback' ),
                ),

                'role_parentid'=>array(

                     array( 'uniqueRoleParentId',G::L( '上级组不能为自己' ),'condition'=>'must','extend'=>'callback' ),
                 ),

            ),

        );

    }


	/**
     * 开启一个查询，查找符合条件的对象或对象集合
     */
    static function F( ){

        $arrArgs = func_get_args();
        return ModelMeta::instance( __CLASS__ )->findByArgs( $arrArgs );
    }


    /**
     * 返回当前 Model 类的元数据对象
     */
    static function M( ){

        return ModelMeta::instance(__CLASS__);
    }


    /**
     * 获取父级角色名字
     */
    public function getParentRoleName( $nRoleId ){

    	if( $nRoleId ==0 ) return;

    	$oRole = self::F( $nRoleId )->getOne();

    	if( !$oRole->isError( ) ){

    		return $oRole->role_name;
    	}
    }


    /**
      * 角色名字唯一性验证
      */
    public function uniqueRoleName(){

    	$nId = Safe::getGpc( 'id' );
    	$sRoleName = trim( Safe::getGpc( 'role_name' ) ) ;
    	$sRoleInfo='';

    	if( $nId ){

    		$arrRole = self::F( 'role_id =?',$nId )->asArray()->getOne( );
    		$sRoleInfo = trim( $arrRole['role_name'] );

    	}

    	if( $sRoleName != $sRoleInfo ){

    	    $arrResult = self::F()->getByrole_name( $sRoleName )->toArray();
    	    if( !empty( $arrResult['role_id'] ) ) {

        	   return false;
            }

            else{

			   return true;
		    }
    	}

    	return true;
    }


    /**
     * 父亲节点验证
     */
    public function uniqueRoleParentId(){

    	$nRoleId = Safe::getGpc( 'id' );
    	$nRoleParentId = Safe::getGpc('role_parentid');

    	if( ( $nRoleId == $nRoleParentId ) && $nRoleId !='' && $nRoleParentId != '' ){

    		return false;
    	}

    	return true;
    }


    /**
     * 获取组应用列表
     */
    public function getGroupAppList( $nGroupId ){

        $oDb = $this->getDb();
		return $oDb->getAllRows( 'select b.node_id,b.node_title,b.node_name from '.
		                               AccessModel::F()->query()->getTablePrefix().'access as a ,'.
		                               NodeModel::F()->query()->getTablePrefix().'node as b where a.node_id=b.node_id and  b.node_parentid=0 and a.role_id='.$nGroupId );

   }

   	/**
	 * 删除组应用列表
	 */
    public function delGroupApp( $nGroupId ){

        $oDb = $this->getDb();

		$bResult = $oDb->query( 'delete from '.AccessModel::F()->query()->getTablePrefix().'access where `access_level`=1 and role_id='.$nGroupId );
		if( $bResult===false ) {

			return false;
		}

		else {

			return true;
		}
	}

	/**
	 * 设置组应用列表
	 */
    public function setGroupApps( $nGroupId,$arrAppIdList ){

    	$oDb = $this->getDb();

		if(empty( $arrAppIdList )) {

			return true;
		}

		$sId = implode( ',',$arrAppIdList );

		$bResult = $oDb->query( 'INSERT INTO '.AccessModel::F()->query()->getTablePrefix().
                                 'access (role_id,node_id,access_parentid,access_level) SELECT a.role_id, b.node_id,b.node_parentid,b.node_level FROM '.
                                  RoleModel::F()->query()->getTablePrefix().'role a, '.
                                  NodeModel::F()->query()->getTablePrefix().
                                  'node b WHERE '.'a.role_id ='.$nGroupId.' AND b.node_id in('.$sId.')' );

		if( $bResult===false ) {

			return false;
		}

		else {

			return true;
		}
	}


	/**
	 * 获取组模块列表
	 */
    public function getGroupModuleList( $nGroupId,$nAppId ){

    	$oDb = $this->getDb();

		return $oDb->getAllRows( 'select b.node_id,b.node_title,b.node_name from '.
		                        AccessModel::F()->query()->getTablePrefix().'access as a ,'.
		                        NodeModel::F()->query()->getTablePrefix().
                                'node as b where a.node_id=b.node_id and  b.node_parentid='.$nAppId.' and a.role_id='.$nGroupId );

	}

	/**
	 * 删除组模块列表
	 */
    public function delGroupModule( $nGroupId,$nAppId ){

    	$oDb = $this->getDb();
		$nResult = $oDb->query( 'delete from '.AccessModel::F()->query()->getTablePrefix().
                                'access where access_level=2 and access_parentid='.$nAppId.' and role_id='.$nGroupId );

		if( $nResult===false ) {

			return false;
		}

		else {

			return true;
		}
	}

	/**
	 * 设置组模块列表
	 */
    public function setGroupModules( $nGroupId,$arrModuleIdList ){

    	$oDb = $this->getDb();

		if( empty($arrModuleIdList )) {

			return true;
		}

		if( is_array( $arrModuleIdList ) ) {

			$arrModuleIdList = implode(',',$arrModuleIdList );
		}

		$bResult = $oDb->query( 'INSERT INTO '.AccessModel::F()->query()->getTablePrefix().
                                'access (role_id,node_id,access_parentid,access_level) SELECT a.role_id, b.node_id,b.node_parentid,b.node_level FROM '.
                                RoleModel::F()->query()->getTablePrefix().'role a, '.NodeModel::F()->query()->getTablePrefix().
                                'node b WHERE '.'a.role_id ='.$nGroupId.' AND b.node_id in('.$arrModuleIdList.')' );

		if( $bResult===false) {

			return false;
		}

		else {

			return true;
		}
	}


   /**
    * 获取组操作列表
    */
   public function getGroupActionList( $nGroupId,$nModuleId ){

       $oDb = $this->getDb();
       return $oDb->getAllRows( 'select b.node_id,b.node_title,b.node_name from '.
                                AccessModel::F()->query()->getTablePrefix().'access as a ,'.
                                NodeModel::F()->query()->getTablePrefix().'node as b where a.node_id=b.node_id and  b.node_parentid='.$nModuleId.' and  a.role_id='.$nGroupId );

   }

   	/**
	 * 删除组操作
	 */
    public function delGroupAction( $nGroupId,$nModuleId ){

    	$oDb = $this->getDb();

        $bResult = $oDb->query( 'delete from '.AccessModel::F()->query()->getTablePrefix().
                                 'access where access_level=3 and access_parentid='.$nModuleId.' and role_id='.$nGroupId );
        if($bResult===false) {

             return false;
        }

        else {

             return true;
        }
    }


    /**
     * 删除组操作
     */
   public function setGroupActions( $nGroupId,$arrActionIdList ){

   	  $oDb = $this->getDb();

      if(empty( $arrActionIdList )) {

          return true;
      }

	  if(is_array( $arrActionIdList )) {

	      $arrActionIdList = implode(',',$arrActionIdList);
	  }

      $bResult = $oDb->query('INSERT INTO '.AccessModel::F()->query()->getTablePrefix().
                             'access (role_id,node_id,access_parentid,access_level) SELECT a.role_id, b.node_id,b.node_parentid,b.node_level FROM '.
                             RoleModel::F()->query()->getTablePrefix().'role a, '.
                             NodeModel::F()->query()->getTablePrefix().'node b WHERE '.'a.role_id ='.$nGroupId.' AND b.node_id in('.$arrActionIdList.')' );

      if( $bResult===false ) {

          return false;
      }

      else {

          return true;
      }

   }

   /**
    * 获取组用户列表
    */
    public function getGroupUserList( $nGroupId ){

    	$oDb = $this->getDb();

		return $oDb->getAllRows( 'select b.user_id,b.user_nikename,b.user_email from '.
		                         userroleModel::F()->query()->getTablePrefix().'userrole as a ,'.
		                         UserModel::F()->query()->getTablePrefix().'user as b where a.user_id=b.user_id and  a.role_id='.$nGroupId );
	}

	/**
	 * 删除组用户列表
	 */
    public function delGroupUser( $nGroupId ){

        $oDb = $this->getDb();

		$bResult = $oDb->query( 'delete from '.userroleModel::F()->query()->getTablePrefix().'userrole where role_id='.$nGroupId );

		if( $bResult === false ) {

			return false;
		}

		else {

			return true;
		}
	}

	/**
	 * 设置组用户列表
	 */
	public function setGroupUsers( $nGroupId,$arrUserIdList ){

		$oDb = $this->getDb();

		if(  empty( $arrUserIdList ) ) {

			return true;
		}

		if( is_string($arrUserIdList ) ) {

			$arrUserIdList = explode( ',',$arrUserIdList );
		}

		array_walk( $arrUserIdList, array( $this, 'fieldFormat' ));

		$arrUserIdList	 =	 implode(',',$arrUserIdList);

		$bResult = $oDb->query( 'INSERT INTO '.userroleModel::F()->query()->getTablePrefix().
                                'userrole (role_id,user_id) SELECT a.role_id, b.user_id FROM '.
                                RoleModel::F()->query()->getTablePrefix().'role a, '.
                                UserModel::F()->query()->getTablePrefix().'user b WHERE '.'a.role_id ='.$nGroupId.' AND b.user_id in('.$arrUserIdList.')' );

		if( $bResult===false ) {

			return false;
		}

		else {

			return true;
		}
	}

	/**
	 * 数据格式化过滤
	 */
    protected function fieldFormat( &$Value ){

        if(is_int( $Value )) {

            $Value = intval( $Value );
        }

        else if( is_float( $Value ) ) {

           $Value = floatval( $Value );
        }

        else if(is_string( $Value )) {

            $Value = '"'.addslashes( $Value ).'"';
        }

        return $Value;
    }


}
